/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 26-Feb-2010, 14:50:30
 */

package phd.dupenois.changeidentifiers.application.graphical.frames;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import phd.dupenois.changeidentifiers.application.graphical.GraphicalApplication;
import phd.dupenois.changeidentifiers.dotpatterns.DotPattern;
import phd.dupenois.changeidentifiers.footprints.Footprint;

/**
 *
 * @author Max
 */
public class MainFrame extends javax.swing.JFrame implements GridListener{
    private Grid grid;
    private StyledDocument outputStyledDoc;
    private GraphicalApplication application;
    /** Creates new form MainFrame
     * @param application
     */
    public MainFrame(GraphicalApplication application) {
        initComponents();
        this.setResizable(true);
        Dimension dimension = new Dimension(800, 600);
        setSize(this, dimension);

        Dimension outputDimension = new Dimension(200, 500);
        setSize(outputPanel, outputDimension);

//        this.statusProgressBar;

        grid = new Grid(this, this.gridContainerPanel, 5);
        this.gridContainerPanel.add(grid);
        this.application = application;
//        grid.setDimensions(dimension);




        outputStyledDoc = outputTextPane.getStyledDocument();
        addStylesToDocument(outputStyledDoc);

        disableReruns();

        this.repaint();

    }
    public void disableReruns(){
        rerunWithSame.setEnabled(false);
        rerunWithDifferent.setEnabled(false);
    }
    public void enableReruns(){
        rerunWithSame.setEnabled(true);
        rerunWithDifferent.setEnabled(true);
    }
    private void setSize(Component component, Dimension dimension){
        component.setSize(dimension);
        component.setPreferredSize(dimension);
        component.setMaximumSize(dimension);
        component.setMinimumSize(dimension);
    }

    public void error(String error){
        appendOutputText(error, "error");
    }
    public void message(String message){
        appendOutputText(message, "message");
    }
    public void warning(String warning){
        appendOutputText(warning, "warning");
    }
    public void success(String success){
        appendOutputText(success, "success");
    }
    private void addStylesToDocument(StyledDocument doc){
        Style def = StyleContext.getDefaultStyleContext().
                        getStyle(StyleContext.DEFAULT_STYLE);

        Style regular = doc.addStyle("regular", def);
        StyleConstants.setFontSize(regular, 12);
        StyleConstants.setFontFamily(def, "SansSerif");

        Style s = doc.addStyle("error", regular);
        StyleConstants.setBold(s, true);
        StyleConstants.setForeground(s, new Color(200, 0, 0, 255));;

        s = doc.addStyle("message", regular);
        StyleConstants.setForeground(s, new Color(0, 0, 200, 255));

        s = doc.addStyle("warning", regular);
        StyleConstants.setItalic(s, true);
        StyleConstants.setForeground(s, new Color(255, 134, 0, 255));

        s = doc.addStyle("success", regular);
        StyleConstants.setBold(s, true);
        StyleConstants.setForeground(s, new Color(47, 165, 61, 255));

    }
    private boolean haltScrollOnManualUse = false;
    public void haltScrollOnManualUse(){
        this.haltScrollOnManualUse = true;
    }
    private void appendOutputText(String text, String stylename){
        //boolean scrollBarAtEnd = (outputTextPane.getCaretPosition()>outputStyledDoc.getLength()-15);
        boolean scrollBarAtEnd = (outputScrollPane.getVerticalScrollBar().getValue()+outputScrollPane.getVerticalScrollBar().getHeight()>
                outputScrollPane.getVerticalScrollBar().getMaximum()-50);
//        System.out.println(outputScrollPane.getVerticalScrollBar().getValue()+" :: "+outputScrollPane.getVerticalScrollBar().getMaximum());
        try {
            outputStyledDoc.insertString(outputStyledDoc.getLength(), text+"\n\n", outputStyledDoc.getStyle(stylename));
        } catch (BadLocationException ex) {
        }
        if(!haltScrollOnManualUse || scrollBarAtEnd){
            outputTextPane.setCaretPosition(outputStyledDoc.getLength());
        }
    }

    public void clearOutputTextPane(){
        try {
            outputStyledDoc.remove(0, outputStyledDoc.getLength() - 1);
        } catch (BadLocationException ex) {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        gridContainerPanel = new javax.swing.JPanel();
        outputPanel = new javax.swing.JPanel();
        outputScrollPane = new javax.swing.JScrollPane();
        outputTextPane = new javax.swing.JTextPane();
        statusPanel = new javax.swing.JPanel();
        statusLabelPanel = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        statusProgressBarPanel = new javax.swing.JPanel();
        statusProgressBar = new javax.swing.JProgressBar();
        graphicalApplicationMenuBar = new javax.swing.JMenuBar();
        graphicalApplicationFileMenu = new javax.swing.JMenu();
        jSeparator1 = new javax.swing.JSeparator();
        rerunWithSame = new javax.swing.JMenuItem();
        rerunWithDifferent = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Change Identifiers");
        setBackground(new java.awt.Color(254, 254, 254));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        gridContainerPanel.setBackground(new java.awt.Color(255, 255, 255));
        gridContainerPanel.setLayout(new java.awt.GridLayout(1, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(gridContainerPanel, gridBagConstraints);

        outputPanel.setLayout(new java.awt.GridLayout(1, 0));

        outputScrollPane.setBackground(new java.awt.Color(254, 254, 254));
        outputScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        outputScrollPane.setOpaque(false);

        outputTextPane.setBackground(new java.awt.Color(255, 255, 255));
        outputTextPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        outputTextPane.setEditable(false);
        outputScrollPane.setViewportView(outputTextPane);

        outputPanel.add(outputScrollPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        getContentPane().add(outputPanel, gridBagConstraints);

        statusPanel.setBackground(new java.awt.Color(255, 255, 255));
        statusPanel.setLayout(new java.awt.GridBagLayout());

        statusLabelPanel.setBackground(new java.awt.Color(255, 255, 255));
        statusLabelPanel.setLayout(new java.awt.GridLayout(1, 0));

        statusLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        statusLabel.setForeground(new java.awt.Color(0, 0, 153));
        statusLabelPanel.add(statusLabel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        statusPanel.add(statusLabelPanel, gridBagConstraints);

        statusProgressBarPanel.setPreferredSize(new java.awt.Dimension(200, 18));

        javax.swing.GroupLayout statusProgressBarPanelLayout = new javax.swing.GroupLayout(statusProgressBarPanel);
        statusProgressBarPanel.setLayout(statusProgressBarPanelLayout);
        statusProgressBarPanelLayout.setHorizontalGroup(
            statusProgressBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
            .addGroup(statusProgressBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(statusProgressBarPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(statusProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        statusProgressBarPanelLayout.setVerticalGroup(
            statusProgressBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
            .addGroup(statusProgressBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(statusProgressBarPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(statusProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        statusPanel.add(statusProgressBarPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
        getContentPane().add(statusPanel, gridBagConstraints);

        graphicalApplicationFileMenu.setText("File");
        graphicalApplicationFileMenu.add(jSeparator1);

        rerunWithSame.setText("Re-Run");
        rerunWithSame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rerunWithSameActionPerformed(evt);
            }
        });
        graphicalApplicationFileMenu.add(rerunWithSame);

        rerunWithDifferent.setText("Re-Run with Different");
        rerunWithDifferent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rerunWithDifferentActionPerformed(evt);
            }
        });
        graphicalApplicationFileMenu.add(rerunWithDifferent);

        graphicalApplicationMenuBar.add(graphicalApplicationFileMenu);

        setJMenuBar(graphicalApplicationMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rerunWithSameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rerunWithSameActionPerformed
        this.application.rerun();
    }//GEN-LAST:event_rerunWithSameActionPerformed

    private void rerunWithDifferentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rerunWithDifferentActionPerformed
        this.application.rerunWithSetup();
    }//GEN-LAST:event_rerunWithDifferentActionPerformed





    public void drawDotPattern(DotPattern dotPattern) {
        grid.setDotPattern(dotPattern);
    }
    public void drawFootprint(Footprint footprint) {
        
        grid.setFootprint(footprint);
    }
    public void updateLocation(int x, int y) {
        setStatus("At: ("+x+", "+y+")");
    }
    public void setStatus(String status) {
        this.statusLabel.setText(status);
    }
    public Point[] getPoints(int index) {
        return null;
    }





    public void setupProgressBar(int total){
        this.statusProgressBar.setMinimum(0);
        this.statusProgressBar.setMaximum(total);
        this.statusProgressBar.setValue(0);
        this.statusProgressBar.repaint();
    }

    public void incrementProgessBar(){
        this.statusProgressBar.setValue(this.statusProgressBar.getValue()+1);
    }
    public void startProgressBarIndeterminate(){
        this.statusProgressBar.setIndeterminate(true);
        this.statusProgressBar.repaint();
    }
    
    public void stopProgressBarIndeterminate(){
        this.statusProgressBar.setIndeterminate(false);
        this.statusProgressBar.repaint();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu graphicalApplicationFileMenu;
    private javax.swing.JMenuBar graphicalApplicationMenuBar;
    private javax.swing.JPanel gridContainerPanel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel outputPanel;
    private javax.swing.JScrollPane outputScrollPane;
    private javax.swing.JTextPane outputTextPane;
    private javax.swing.JMenuItem rerunWithDifferent;
    private javax.swing.JMenuItem rerunWithSame;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JPanel statusLabelPanel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JProgressBar statusProgressBar;
    private javax.swing.JPanel statusProgressBarPanel;
    // End of variables declaration//GEN-END:variables



}
